package cn.mugutu.finance.controller;

import cn.mugutu.finance.annotation.NoAuthenticationAPI;
import cn.mugutu.finance.annotation.ParamValidate;
import cn.mugutu.finance.annotation.SysLog;
import cn.mugutu.finance.annotation.SysResource;
import cn.mugutu.finance.aop.ParamsValidateAop;
import cn.mugutu.finance.domain.system.SystemConfig;
import cn.mugutu.finance.domain.system.User;
import cn.mugutu.finance.dto.BaseResult;
import cn.mugutu.finance.dto.EasyUIResult;
import cn.mugutu.finance.dto.UserDto;
import cn.mugutu.finance.exception.BusinessException;
import cn.mugutu.finance.query.UserQuery;
import cn.mugutu.finance.service.system.SystemConfigService;
import cn.mugutu.finance.service.system.UserService;
import cn.mugutu.finance.utils.ConstantConfig;
import cn.mugutu.finance.utils.Pager;
import cn.mugutu.finance.vo.UpdatePaswordReqVO;
import cn.mugutu.finance.vo.UserAddReqVO;
import cn.mugutu.finance.vo.UserLoginReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * userController
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private SystemConfigService sysConfigService;

    @RequestMapping("/login")
    @ResponseBody
    @NoAuthenticationAPI
    public BaseResult login(@ParamValidate UserLoginReqVO vo) {
        String username = vo.getUsername();
        String password = vo.getPassword();
        String randomcode = vo.getRandomcode();
        return userService.login(username, password, randomcode, request);
    }

    public static void main(String[] args) {
        String all = "Al " ;
        System.out.println(all.intern());
    }

    @RequestMapping("/loginUI")
    @NoAuthenticationAPI
    public String loginUI() {
        SystemConfig systemConfig = sysConfigService.get(1L);
        if (systemConfig != null) {
            session.setAttribute(ConstantConfig.SYSCONFIG_INFO, systemConfig);
        }
        return "login";
    }

    @RequestMapping("/logout")
    public String logout() {
        session.invalidate();
        return "redirect:/user/loginUI";
    }

    @RequestMapping("/listUI")
    @SysResource(name = "用户管理", code = "user:userlist")
    public String listUI() {
        return "user/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public EasyUIResult<UserDto> list(UserQuery query, int page, int rows) {
        setEasyUIParamsInQuery(query, page, rows);
        Pager<User> pager = userService.findByPage(query);
        List<UserDto> dtoList = UserDto.userListToUserDtoList(pager.getRows());
        return new EasyUIResult<UserDto>(pager.getTotalCount(), dtoList);
    }

    @RequestMapping("/get")
    @ResponseBody
    public User getById(Long id) {
        return userService.get(id);
    }

    @RequestMapping("/updatepasswordUI")
    public String updatePasswordUI() {
        return getIndexPage() + "/updatepassword";
    }

    @RequestMapping("/updatepassword")
    @ResponseBody
    @SysLog(description = "修改密码")
    public BaseResult updatePassword(@ParamValidate UpdatePaswordReqVO vo) {
        return userService.updatePassword(vo.getOldPassword(), vo.getNewPassword(), vo.getNewPassword(), getLoginUser());
    }

    //解锁/锁定用户
    @RequestMapping("/lock")
    @ResponseBody
    @SysResource(name = "锁定/解锁用户", code = "user:lock")
    @SysLog(description = "锁定/解锁用户")
    public BaseResult lockUser(Long id) {
        return userService.lockUser(id);
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @SysResource(name = "新增/修改用户", code = "user:saveorupdate")
    public BaseResult saveOrUpdate(UserAddReqVO userAddReqVO) {
        return userService.saveOrUpdateUser(userAddReqVO);
    }

    @RequestMapping("/delete")
    @ResponseBody
    @SysLog(description = "删除用户")
    @SysResource(name = "删除用户", code = "user:delete")
    public BaseResult delete(Long id) {
        User loginUser = getLoginUser();
        if (loginUser.getId() == id) {
            return new BaseResult(false, "你不能删除你自己");
        }
        try {
            User user = userService.get(id);
            if ("admin".equals(user.getUsername())) {
                return new BaseResult(false, "不能删除管理员账户");
            } else {
                userService.delete(id);
                return new BaseResult(true, "删除成功");
            }
        } catch (Exception e) {
            throw new BusinessException("删除用户失败:" + e.getMessage());
        }
    }

    /**
     * 无权访问
     * @return
     */
    @RequestMapping("/noPrivilegeAccessUI")
    @NoAuthenticationAPI
    public String noPrivilegeAccessUI() {
        return "user/noPrivilege";
    }

}
